package com.hsy.swu.home.textbook

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.setMargins
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.internal.ViewUtils.dpToPx
import com.hsy.swu.databinding.ItemTextbookBinding
import com.hsy.swu.hsyutils.MActivityUtil
import com.hsy.swu.hsyutils.setFont

class TextbookAdapter : RecyclerView.Adapter<TextbookAdapter.TextbookViewHolder>() {
    var textbookList = emptyList<TextbookModel>()
        @SuppressLint("NotifyDataSetChanged")
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TextbookViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val itemBinding = ItemTextbookBinding.inflate(layoutInflater, parent, false)
        return TextbookViewHolder(itemBinding)
    }

    override fun getItemCount(): Int {
        return textbookList.size
    }

    override fun onBindViewHolder(holder: TextbookViewHolder, position: Int) {
        holder.bind(textbookList[position], position == 0 || position == 1)
    }

    class TextbookViewHolder(private val binding: ItemTextbookBinding) :
        RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("RestrictedApi")
        fun bind(textbookModel: TextbookModel, isNeedMarginTop: Boolean) {
            binding.apply {
                val lp = binding.root.layoutParams as ViewGroup.MarginLayoutParams
                if (isNeedMarginTop) {
                    lp.setMargins(
                        dpToPx(binding.root.context, 5).toInt(),
                        dpToPx(binding.root.context, 35).toInt(),
                        dpToPx(binding.root.context, 5).toInt(),
                        dpToPx(binding.root.context, 5).toInt(),
                    )
                } else {
                    lp.setMargins(dpToPx(binding.root.context, 5).toInt())
                }
                binding.root.layoutParams = lp

                tvCourseName.text = textbookModel.courseName
                if (textbookModel.textBookName == "无教材") {
                    tvTextbookName.text = textbookModel.textBookName
                    for (i in root.childCount - 1 downTo 3) {
                        root.getChildAt(i).visibility = View.GONE // 移除教材名后的 View
                    }
                    root.setOnClickListener {
                        MActivityUtil.showToast("无教材信息")
                    }
                } else {
                    tvTextbookName.text = "《${textbookModel.textBookName}》"
                    tvIsbn.text = textbookModel.isbn
                    tvAuthor.text = textbookModel.author
                    tvPublisher.text = textbookModel.publisher
                    tvVersion.text = textbookModel.version
                    val sb = StringBuilder()
                    textbookModel.apply {
                        sb.append("$courseName    ")
                        sb.append("《$textBookName》    ")
                        sb.append("$isbn    ")
                        sb.append("$author    ")
                        sb.append("$publisher    ")
                        sb.append(version)
                    }
                    for (i in root.childCount - 1 downTo 3) {
                        root.getChildAt(i).visibility = View.VISIBLE // 显示教材名后的 View
                    }
                    root.setOnClickListener {
                        MActivityUtil.clipText(it.context, sb.toString(), "教材信息已复制到剪贴板")
                    }
                }
                setFont()
            }
        }
    }
}